package asionreachel.cn.smarthomedemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.widget.ValidatePhoneView;
import familyclient.android.familymanager.JFamilyManagerInterface;
import familyclient.android.familymanager.JFamilyManagerListener;

public class ForgotPasswordActivity extends BaseActivity {
    private static final String TAG = "ForgotPasswordActivity";
    private static final String FUNCTION = "ForgotPassword";
    public static final int s_iRequestCodeResetPassword = 0;

    private EditText m_etAccount;
    private EditText m_etVerifyCode;
    private EditText m_etPassword1;
    private EditText m_etPassword2;
    private Button m_btSave;

    private JFamilyManagerListener m_listenerFamilyManager = new JFamilyManagerListener()
    {
        @Override   //通过旧密码修改密码返回
        public void ModPwdByOldPwdCb(String a_strErr) {
            if( !m_bRun )
                return;

            if( null != a_strErr && a_strErr.length() > 0 )
            {
                // 通过手机修改密码失败
                Common.showToast( Common.MakeMsgByErr( FUNCTION, a_strErr ) );
            }
            else
            {
                // 通过手机修改密码成功
                Common.showToast(R.string.toast_modify_password_success);
                onSaveCall();
            }
        }
    };

    public void onBack() {
        finish();
    }

    @Override
    public void onBackKey() {
        onBack();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView( R.layout.activity_forgot_password );
        //加入监听
        JFamilyManagerInterface.GetInstance().AddListener( m_listenerFamilyManager );
        ImageView ivBack = (ImageView)findViewById(R.id.iv_back);
        m_etPassword1 = (EditText)findViewById(R.id.forgot_ed_pass1);
        m_etPassword2 = (EditText)findViewById(R.id.forgot_ed_pass2);
        m_btSave = (Button)findViewById(R.id.forgot_btn_yes);


        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
        m_btSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSave();
            }
        });
    }

    //请求下一步
    private void onSave()
    {
        String strPassword1 = m_etPassword1.getText().toString();
        String strPassword2 = m_etPassword2.getText().toString();
        if (strPassword1.isEmpty() || strPassword2.isEmpty())
        {
            Log.d(TAG , "密码不能为空");
            Common.showToast(R.string.toast_null_password);
            return;
        }
        else if (!strPassword1.equals(strPassword2))
        {
            Common.showToast(R.string.toast_different_password);
            return;
        }
        //修改密码接口
        JFamilyManagerInterface.GetInstance().ModPwdByOldPwd( strPassword1, strPassword2);

    }

    private void onSaveCall()
    {
        finish();
    }

    @Override
    protected void onDestroy()
    {
        //注销监听
        JFamilyManagerInterface.GetInstance().DelListener( m_listenerFamilyManager );
        super.onDestroy();
    }
}
